package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.Answer;
import cn.wolfcode.trip.base.domain.AnswerComment;
import cn.wolfcode.trip.base.query.AnswerCommentQuery;
import cn.wolfcode.trip.base.query.AnswerQuery;
import cn.wolfcode.trip.base.service.IAnswerCommentService;
import cn.wolfcode.trip.base.service.IAnswerService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/answers")
public class AnswerController {

    @Autowired
    private IAnswerService answerService;
    @Autowired
    private IAnswerCommentService answerCommentService;

    /**
     * 根据问题id获取回答者信息
     * 资源 /{questionId}
     * 请求方式 get
     * 请求参数 questionId
     * 返回结果 pageInfo
     */
    @GetMapping("/{questionId}")
    public PageInfo selectAnswerByQueestionId(AnswerQuery qo,@PathVariable("questionId")Long questionId){
     return answerService.selectAnswerByQueestionId(qo,questionId);
     }
     /**
     * 保存指定问题的回答信息
     * 资源 /{questionId}
     * 请求方式 post
     * 请求参数 questionId
     * 返回结果 JsonResult
     */
    @PostMapping("/{questionId}")
    public JsonResult saveReplyByQuestionId(Answer answer,@PathVariable("questionId") Long questionId){
        JsonResult result = new JsonResult();
        answerService.saveReplyByQuestionId(answer,questionId);
        return result;
    }

    /**
     * 查询指定提问信息
     * 资源 /{answers}/{id}
     * 请求方式 get
     * 请求参数 id
     * 返回结果 question
     */
    @GetMapping("/{id}/nums")
    public Answer selectById(@PathVariable Long id){
        return answerService.get(id);
    }
    /**
     * 根据问题id获取回答者信息
     * 资源 /{answerId}
     * 请求方式 get
     * 请求参数 answerId
     * 返回结果 pageInfo
     */
    @GetMapping("/{answerId}/comments")
    public PageInfo selectComentByAnswerId(AnswerCommentQuery qo, @PathVariable("answerId")Long answerId){
        return answerCommentService.selectForList(qo,answerId);
    }
    /**
     * 根据问题id保存评论信息
     * 资源 /comments
     * 请求方式 Post
     * 请求参数 answerId
     * 返回结果 pageInfo
     */
    @PostMapping("/comments/{answerId}")
    public JsonResult saveCommentByanswerId(AnswerComment answerComment,@PathVariable("answerId") Long answerId){
        JsonResult result = new JsonResult();
        answerCommentService.saveCommentByanswerId(answerComment,answerId);
        return result;
    }
    /**
     * 根据questionId查询回答信息的总条数
     * 资源 /{questionId}
     * 请求方式 get
     * 请求参数 questionId
     * 返回结果 int
     */
    @GetMapping("/{questionId}/answerNums")
    public int countAnswerNums(@PathVariable("questionId") Long questionId){
        return answerService.countAnswerNums(questionId);
    }

}
